package com.mayikt.days08;

/**
 * @ClassName Test002
 * @Author 蚂蚁课堂余胜军 QQ644064779 www.mayikt.com
 * @Version V1.0
 **/
public class Test002 {
//    /**
//     * 共享的变量
//     */
//    private String context;

    public String getContext() {
        return threadLocal.get();
    }

    public void setContext(String context) {
        threadLocal.set(context);
    }

    private static ThreadLocal<String> threadLocal = new ThreadLocal<>();

    private static Object lock = new Object();

    public static void main(String[] args) {
        Test002 test002 = new Test002();
        for (int i = 0; i < 5; i++) {
            int finalI1 = i;
            new Thread(() -> {
                test002.setContext(Thread.currentThread().getName() + "," + finalI1);
                try {
                    Thread.sleep(500);
                } catch (Exception e) {
                }
                // 读操作
                System.out.println(test002.getContext());
            }, i + "").start();
        }
    }
}
